<template>
  <div class="app-container">
    <!-- 搜索框 -->
    <div class="search-container">
      <el-form :model="searchParam" :inline="true">
        <el-form-item label="角色名称">
          <el-input
            v-model="searchParam.roleName"
            placeholder="请输入角色名称"
            clearable
            @keyup.enter="search"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="search">
            <i-ep-search />
            搜索
          </el-button>
          <el-button @click="reset">
            <i-ep-refresh />
            重置
          </el-button>
        </el-form-item>
      </el-form>
    </div>
    <!-- 列表 -->
    <el-card shadow="never" class="table-container">
      <template #header>
        <el-button type="success" @click="jumpPage()">
          <i-ep-plus />
          页面新建
        </el-button>
        <el-button type="success" plain @click="openDialog()">
          <i-ep-plus />
          弹窗新增
        </el-button>
        <el-button
          type="danger"
          plain
          :disabled="ids.length === 0"
          :loading="deleteLoading"
          @click="handleTableDelete()"
        >
          <i-ep-delete />
          删除
        </el-button>
      </template>

      <el-table
        ref="dataTableRef"
        v-loading="loading"
        :data="tableData"
        highlight-current-row
        border
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column
          label="角色名称"
          prop="roleName"
          min-width="150"
          show-overflow-tooltip
        />
        <el-table-column
          label="备注"
          prop="remarks"
          width="150"
          show-overflow-tooltip
        />

        <el-table-column label="状态" align="center" width="100">
          <template #default="scope">
            <el-tag v-if="scope.row.enabled === 1" type="success">正常</el-tag>
            <el-tag v-else type="info">停用</el-tag>
          </template>
        </el-table-column>

        <el-table-column fixed="right" label="操作" width="160">
          <template #default="scope">
            <el-button
              type="primary"
              size="small"
              link
              @click="openMenuDialog(scope.row)"
            >
              <i-ep-position />
              分配
            </el-button>
            <el-button
              type="primary"
              size="small"
              link
              @click="openDialog(scope.row)"
            >
              <i-ep-edit />
              编辑
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <el-pagination
        small
        :current-page="pageable.pageNum"
        :page-size="pageable.pageSize"
        :page-sizes="[10, 25, 50, 100]"
        :total="pageable.total"
        layout="total, sizes, prev, pager, next, jumper"
        class="mt-[20px]"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </el-card>
    <!-- 新增 -->
    <add-role
      :visible="addDialog.visible"
      :title="addDialog.title"
      :data="addDialog.data"
      @reset-table="(id:string) => (id?.length ? getTableList() : reset())"
      @change-visible="changeVisible"
    />
    <!-- 分配 -->
    <up-role-menu
      :visible="addMenuDialog.visible"
      :title="addMenuDialog.title"
      :data="addMenuDialog.data"
      :id="addMenuDialog.id"
      @reset-table="getTableList"
      @change-visible="changeVisibleMenu"
    />
  </div>
</template>

<script setup lang="ts">
import router from "@/router";
import { useTable } from "@/utils/useTable";
import request from "@/utils/request";

defineOptions({
  name: "WINDOW7",
  inheritAttrs: false,
});

// 请求参数
const requestApi = "/system/sysRole/queryByPage";

// 是否需要分页
const pagination = true;

const initParam = {};

// 初始化搜索参数数据
const searchParamInit = {
  roleName: "",
};

const deleteParam = {
  api: "/system/sysRole/delete",
  data: {},
};

// 如果初始化表格需要排序=》const initParam = reactive({sort:''});
// 监听页面 initParam 改化，重新获取表格数据
// watch(() => initParam, getTableList, { deep: true });

// 数据请求错误后处理
function requestError() {}

// 数据请求错误后处理
// function dataCallBack() {}   //需要处理
const dataCallBack = ref(undefined); //无需二次数据处理&不传赞数也行

// 表格操作 Hooks
const {
  tableData,
  pageable,
  searchParam,
  ids,
  loading,
  getTableList,
  search,
  reset,
  handleSizeChange,
  handleCurrentChange,
  handleSelectionChange,
  handleTableDelete,
  deleteLoading,
}: any = useTable(
  requestApi,
  initParam,
  searchParamInit,
  pagination,
  dataCallBack.value,
  requestError,
  deleteParam
);

onMounted(() => {
  getTableList();
  getMeuList();
});

/** 新建页面跳转 */
function jumpPage() {
  router.push({ path: "/demo/window7/add" });
}

/** 打开角色表单弹窗 */
const addDialog = reactive<DialogOption>({
  visible: false,
  title: "",
  data: {},
});

async function getSystemRole(id: string) {
  const { data } = await request({
    url: `/system/sysRole/get/${id}`,
    method: "get",
  });
  addDialog.data = data;
}

function openDialog(roleObj?: any) {
  if (roleObj) {
    addDialog.title = "修改角色";
    getSystemRole(roleObj.id);
  } else {
    addDialog.title = "新增角色";
    addDialog.data = {};
  }
  addDialog.visible = true;
}

function changeVisible(value: boolean) {
  addDialog.visible = value;
}

/** 打开权限分配按钮 */
const addMenuDialog = reactive<DialogOption>({
  visible: false,
  title: "",
  id: "",
  data: [],
});

const selectMenuList = ref([]);
function openMenuDialog(roleObj?: any) {
  addMenuDialog.visible = true;
  addMenuDialog.title = `【${roleObj.roleName}】分配权限`;
  addMenuDialog.data = selectMenuList.value;
  addMenuDialog.id = roleObj.id;
}

function changeVisibleMenu(value: boolean) {
  addMenuDialog.visible = value;
}

async function getMeuList() {
  const { data } = await request({
    url: "/system/sysPermission/roleTree",
    method: "post",
  }).finally(() => {});
  selectMenuList.value = data;
}
</script>
